
IF (DEFINED VERNUMBER)
  SET(TD_VER_NUMBER ${VERNUMBER})
ELSE ()
  SET(TD_VER_NUMBER "3.3.8.8.alpha")
ENDIF ()

IF (DEFINED VERCOMPATIBLE)
  SET(TD_VER_COMPATIBLE ${VERCOMPATIBLE})
ELSE ()
  SET(TD_VER_COMPATIBLE "3.0.0.0")
ENDIF ()

IF (TD_PRODUCT_NAME)
  ADD_DEFINITIONS(-DTD_PRODUCT_NAME="${TD_PRODUCT_NAME}")
ENDIF ()

IF (CUS_NAME)
  ADD_DEFINITIONS(-DCUS_NAME="${CUS_NAME}")
ENDIF ()

IF (CUS_PROMPT)
  ADD_DEFINITIONS(-DCUS_PROMPT="${CUS_PROMPT}")
ENDIF ()

IF (CUS_EMAIL)
  ADD_DEFINITIONS(-DCUS_EMAIL="${CUS_EMAIL}")
ENDIF ()

find_program(HAVE_GIT NAMES git)

IF (DEFINED GITINFO)
  SET(TD_VER_GIT ${GITINFO})
ELSEIF (HAVE_GIT)
  message(STATUS "TD_COMMUNITY_DIR:${TD_COMMUNITY_DIR}")
  execute_process(COMMAND git log -1 --format=%H WORKING_DIRECTORY ${TD_COMMUNITY_DIR} OUTPUT_VARIABLE GIT_COMMITID)
  #message(STATUS "git log result:${GIT_COMMITID}")
  IF (GIT_COMMITID)
    string (REGEX REPLACE "[\n\t\r]" "" GIT_COMMITID ${GIT_COMMITID})
    SET(TD_VER_GIT ${GIT_COMMITID})
  ELSE ()
    message(STATUS "not a git repository")
    SET(TD_VER_GIT "no git commit id")
  ENDIF ()
ELSE ()
  message(STATUS "no git found")
  SET(TD_VER_GIT "no git commit id")
ENDIF ()

IF (DEFINED GITINFOI)
  SET(TD_VER_GIT_INTERNAL ${GITINFOI})
ELSEIF (HAVE_GIT AND TD_ENTERPRISE)
  execute_process(COMMAND git log -1 --format=%H WORKING_DIRECTORY ${TD_ENTERPRISE_DIR} OUTPUT_VARIABLE GIT_COMMITID)
  message(STATUS "git log result:${GIT_COMMITID}")
  IF (GIT_COMMITID)
    string (REGEX REPLACE "[\n\t\r]" "" GIT_COMMITID ${GIT_COMMITID})
    SET(TD_VER_GIT_INTERNAL ${GIT_COMMITID})
  ELSE ()
    message(STATUS "not a git repository")
    SET(TD_VER_GIT "no git commit id")
  ENDIF ()
ELSEIF (HAVE_GIT)
  message(STATUS "PROJECT_SOURCE_DIR:${PROJECT_SOURCE_DIR}")
  execute_process(COMMAND git log -1 --format=%H WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} OUTPUT_VARIABLE GIT_COMMITID)
ELSE ()
  message(STATUS "no git cmd")
  SET(TD_VER_GIT_INTERNAL "no git commit id")
ENDIF ()

message(STATUS "TD_VER_GIT_INTERNAL: ${TD_VER_GIT_INTERNAL}")
message(STATUS "TD_VER_GIT: ${TD_VER_GIT}")

IF (DEFINED VERDATE)
  SET(TD_VER_DATE ${VERDATE})
ELSE ()
  STRING(COMPARE GREATER_EQUAL "${CMAKE_VERSION}" "3.26" TD_CMAKE_SUPPORT_TZ)

  IF (TD_CMAKE_SUPPORT_TZ)
    STRING(TIMESTAMP TD_VER_DATE "%Y-%m-%d %H:%M:%S %z")
  ELSE ()
    IF (TD_WINDOWS)
      STRING(TIMESTAMP TD_VER_DATE "%Y-%m-%d %H:%M:%S")
    ELSE ()
      EXECUTE_PROCESS(COMMAND date +"%F %T %z" OUTPUT_VARIABLE TD_VER_DATE)
      STRING(REPLACE "\"" "" TD_VER_DATE ${TD_VER_DATE})
      STRING(STRIP ${TD_VER_DATE} TD_VER_DATE)
    ENDIF ()
  ENDIF ()
ENDIF ()

IF (DEFINED VERTYPE)
  SET(TD_VER_VERTYPE ${VERTYPE})
ELSE ()
  SET(TD_VER_VERTYPE "stable")
ENDIF ()

IF (DEFINED CPUTYPE)
  SET(TD_VER_CPUTYPE ${CPUTYPE})
ELSE ()
  IF (TD_WINDOWS_32)
    SET(TD_VER_CPUTYPE "x86")
  ELSEIF (TD_LINUX_32)
    SET(TD_VER_CPUTYPE "x86")  
  ELSEIF (TD_ARM_32)
    SET(TD_VER_CPUTYPE "arm32")
  ELSEIF (TD_MIPS_32)
    SET(TD_VER_CPUTYPE "mips32")  
  ELSE ()
    SET(TD_VER_CPUTYPE "x64")
  ENDIF ()
ENDIF ()

IF (DEFINED OSTYPE)
  SET(TD_VER_OSTYPE ${OSTYPE})
ELSE ()
  SET(TD_VER_OSTYPE "Linux")
ENDIF ()

MESSAGE(STATUS "============= compile version parameter information start ============= ")
MESSAGE(STATUS "version:    " ${TD_VER_NUMBER})
MESSAGE(STATUS "compatible: " ${TD_VER_COMPATIBLE})
MESSAGE(STATUS "commit id:  " ${TD_VER_GIT})
MESSAGE(STATUS "build date: " ${TD_VER_DATE})
MESSAGE(STATUS "build type: " ${CMAKE_BUILD_TYPE})
MESSAGE(STATUS "type:       " ${TD_VER_VERTYPE})
MESSAGE(STATUS "cpu:        " ${TD_VER_CPUTYPE})
MESSAGE(STATUS "os:         " ${TD_VER_OSTYPE})
MESSAGE(STATUS "============= compile version parameter information end  ============= ")

STRING(REPLACE "." "_" TD_LIB_VER_NUMBER ${TD_VER_NUMBER})
